package cn.itcast.myapplication.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cn.itcast.myapplication.R;
import cn.itcast.myapplication.room.Word;
import cn.itcast.myapplication.viewemodel.WordViewModel;

public class AddActivity extends AppCompatActivity {

     private EditText editText_chinese;

     private EditText editText_english;

     private Button button;

     private WordViewModel wordViewModel;

     private String chinese;

     private String english;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        wordViewModel=new ViewModelProvider(this).get(WordViewModel.class);//创建ViewModel对象

        editText_chinese=(EditText)findViewById(R.id.editText_chinese);

        editText_english=(EditText) findViewById(R.id.editText_english);

        button=(Button)findViewById(R.id.add_Insert_chinese);

        button.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {

               chinese=editText_chinese.getText().toString();

               english=editText_english.getText().toString();

               if (chinese.equals("") || english.equals("")){

                   button.setClickable(true);

                   Toast.makeText(AddActivity.this,"输入框不能为空，请输入你要提交的单词",Toast.LENGTH_SHORT).show();

               }else {

                   button.setClickable(false);

                   Word word=new Word(english,chinese);

                   wordViewModel.insertWord(word);

                   Toast.makeText(AddActivity.this,"添加\n"+chinese+"\n"+english+"\n"+"成功",Toast.LENGTH_SHORT).show();

                   finish();

               }

           }

       });

    }

}

